package com.creating.mict.dao;


import java.util.ArrayList;
import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.creating.mict.domain.User;

/**
 * 
* @ClassName: UserDao 
* @Description: 用户Dao
* @author xuelei
* @date 2012-3-7 下午5:24:18 
*
 */
public class UserDao extends HibernateDaoSupport{

	private static final Log log = LogFactory.getLog(UserDao.class);
	
	/**
	 * 
	* @Title: getUserByLoginName 
	* @Description: 根据登录名获取用户
	* @param @param loginName
	* @param @return 
	* @return User    返回类型 
	* @throws
	 */
	public User getUserByLoginName(String loginName) {
		String sql = "";
		sql = " select FLOGINNAME, FPASSWORD, FTYPE, FSTATE, FLDATE, foffice, fname, fphone "
				+ " from TDS_USER " + " where FLOGINNAME='" + loginName + "' ";

		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		if(list.size() <= 0) {
			return null;
		}
		User user = new User();
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			for (int j = 0; j < obj.length; j++) {
				user.setLoginName("" + obj[0]);
				user.setPassword("" + obj[1]);
				user.setFtype("" + obj[2]);
				user.setFstate("" + obj[3]);
				user.setLastDateStr("" + obj[4]);
				user.setFoffice("" + obj[5]);
				user.setFname("" + obj[6]);
				user.setFphone("" + obj[7]);
			}
		}
		return user;
	}
	
	/**
	 * 
	* @Title: getUserList 
	* @Description: 获取用户列表
	* @param @return 
	* @return List<User>    返回类型 
	* @throws
	 */
	public List<User> getUserList(String officeCode) {
		String sql = "";
		sql = " select a.FLOGINNAME, a.FPASSWORD, a.FTYPE, a.FSTATE, a.FLDATE, a.fname, a.fphone "
				+ " from TDS_USER a " 
				+ " where (a.FTYPE='2' or a.FTYPE='3') and foffice='" + officeCode + "'" 
				+ " order by fname asc ";
		Query query = this.getSession().createSQLQuery(sql);
		List list = query.list();
		List<User> u_list = new ArrayList<User>();
		
		if(list.size() <= 0) {
			return null;
		}
		
		for (int i = 0; i < list.size(); i++) {
			Object[] obj = (Object[]) list.get(i);
			User user = new User();
			for (int j = 0; j < obj.length; j++) {
				user.setLoginName("" + obj[0]);
				user.setPassword("" + obj[1]);
				user.setFtype("" + obj[2]);
				user.setFstate("" + obj[3]);
				user.setLastDateStr("" + obj[4]);
				user.setFname("" + obj[5]);
				user.setFphone("" + obj[6]);
			}
			u_list.add(user);
		}
		return u_list;
	}
	
	/**
	 * 
	* @Title: saveUser 
	* @Description: 保存用户
	* @param @param user
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveUser(User user) {
		try {
			getHibernateTemplate().saveOrUpdate(user);
			getHibernateTemplate().flush();
			return true;
		} catch (RuntimeException re) {
			log.error("保存用户失败！", re);
			//throw re;
			return false;
		}
	}
	
	/**
	 * 
	* @Title: deleteUserById 
	* @Description: 删除用户
	* @param @param loginName
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean deleteUserById(String loginName) {
		try {
			String sql = "delete TDS_USER a " 
					   + " where a.floginname='" + loginName + "'";
			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return false;
			}
		} catch (RuntimeException re) {
			log.error("删除用户失败！", re);
			throw re;
		}
	}
	
	/**
	 * 
	* @Title: saveUserRole 
	* @Description: 保存用户角色
	* @param @param loginName
	* @param @param roleId
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean saveUserRole(String loginName, String roleId) {
		try {
			//先清空
			clearUserRole(loginName);
			//再写入
			String sql = "";
			sql = "insert into TDS_USER_ROLE(fuser, frole) " 
					   + " values('" + loginName + "', '" + roleId + "')";
			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return false;
			}
		} catch (RuntimeException re) {
			log.error("保存角色对应栏目失败！", re);
			//throw re;
			return false;
		}
	}
	
	/**
	 * 
	* @Title: clearUserRole 
	* @Description: 清空用户角色
	* @param @param loginName
	* @param @return 
	* @return boolean    返回类型 
	* @throws
	 */
	public boolean clearUserRole(String loginName) {
		try {
			String sql = "delete TDS_USER_ROLE a " 
					   + " where a.fuser='" + loginName + "'";
			Query query = this.getSession().createSQLQuery(sql);
			int num = query.executeUpdate();
			if (num > 0) {
				return true;
			} else {
				return false;
			}
		} catch (RuntimeException re) {
			log.error("清空用户角色失败！", re);
			throw re;
		}
	}
}